const { UUIDV4, UUID, STRING, TINYINT, DATE, Model } = require('sequelize');
const { checkThePhone } = require('../../helpers/validator');
const db = require('../../helpers/db');

class Address extends Model {}

Address.init(
    {
        id: {
            type: UUID,
            defaultValue: UUIDV4,
            primaryKey: true,
            comment: '收货地址ID'
        },
        user: {
            type: UUID,
            unique: true,
            comment: '外键，用户ID'
        },
        contact: {
            type: STRING(16),
            allowNull: false,
            comment: '联系人'
        },
        phone: {
            type: STRING(11),
            unique: true,
            comment: '用户联系电话',
            allowNull: false,
            validate: {
                customValidator(value) {
                    if (value && !checkThePhone(value)) {
                        throw new Error('请输入正确的手机号码');
                    }
                }
            }
        },
        province: {
            type: STRING(32),
            allowNull: false,
            comment: '省市信息'
        },
        city: {
            type: STRING(32),
            allowNull: false,
            comment: '城市信息'
        },
        county: {
            type: STRING(32),
            allowNull: false,
            comment: '区域信息'
        },
        detailAddress: {
            type: STRING(32),
            comment: '详细地址',
            allowNull: false,
            field: 'detail_address'
        },
        remark: {
            type: STRING(32),
            comment: '备注'
        },
        def: {
            type: TINYINT(1),
            comment: '默认地址'
        }
    },
    {
        tableName: 'address',
        underscored: true,
        sequelize: db
    }
);

module.exports = Address;
